//
//  CategoryTopicDetailViewController.m
//  liwushuo
//
//  Created by qf1 on 16/4/15.
//  Copyright (c) 2016年 ZJH. All rights reserved.
//

#import "CategoryTopicDetailViewController.h"
#import "GuideContentTableViewCell.h"
#import "DetailViewController.h"

@interface CategoryTopicDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (retain,nonatomic)NSMutableArray * dataArray;
@property (retain,nonatomic)UITableView * tableView;

@end

#import "CategoryTopicDetailViewController.h"
#import "GuideContentTableViewCell.h"


@implementation CategoryTopicDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载";
    hud.detailsLabelText = @"请稍后";
    [hud show:YES];
    [self createView];
}

- (void)createView{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, theScreenWidth, theScreenHeight - 64) style:UITableViewStylePlain];
    _tableView.rowHeight = theScreenWidth * 2 / 5;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.bounces = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"GuideContentTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:_tableView];
    [self getData];
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)getData{
    NetWorkManager * manager = [[NetWorkManager alloc] init];
    __weak CategoryTopicDetailViewController * weakself = self;
    [manager getAdvContentDataWithUrl:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections/%d/posts?limit=20&offset=0",[_banner.Id intValue]] andBlock:^(id respond) {
        _dataArray = [[NSMutableArray alloc] initWithArray:respond];
        [_tableView reloadData];
        [MBProgressHUD hideAllHUDsForView:weakself.view animated:YES];
    }];
    
}

//- (void)callBackWithContentData:(NSArray *)array{
//    _dataArray = [[NSMutableArray alloc] initWithArray:array];
//    [_tableView reloadData];
//}


#pragma mark 列表的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellid = @"cell";
    ContentItem * item = _dataArray[indexPath.row];
    GuideContentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    [cell.backGroundImage setImageWithURL:[NSURL URLWithString:item.cover_image_url]];
    CALayer * layer = cell.backGroundImage.layer;
    layer.cornerRadius = 5;
    layer.masksToBounds = YES;
    cell.title.text = item.title;
    cell.favouriteLabel.text = [NSString stringWithFormat:@"%d",[item.likes_count intValue]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ContentItem * item = _dataArray[indexPath.row];
    DetailViewController * vc = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:NO];
    vc.Id = item.Id;
    
}

- (void)viewWillAppear:(BOOL)animated{
    [self.tabBarController.tabBar setHidden:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
